$(function () {

    var $searchResult = $('#searchResult')
        , $searchLabels = $('#searchLabels')
        , saveSearchResult = [];


    $searchLabels.find('.search-labels-item').on('tap', function () {
        var searchContent = $(this).text();
        $('#searchBar').addClass('weui-search-bar_focusing');
        $('#searchInput').val(searchContent)
        getSearchPoints(searchContent, function (body) {
            if (body.data && body.data.rows.length > 0) {
                saveSearchResult = body.data.rows; 
                $searchResult.html(liveHtmlChange(body.data.rows));
                bindSearchItemTap();
            } else {
                $searchResult.html(liveHtmlChange([]));
            }
            body = null;
        })
        $searchLabels.hide();
        $searchResult.show();
    })

    $('#searchInput').live('input propertychange', function () {
        var $this = $(this);
        var thisContent = $this.val();

        if (thisContent === '') {
            $searchResult.hide().html('');
            $searchLabels.show();
        } else {
            getSearchPoints(thisContent, function (body) {
                if(body.data && body.data.rows.length) {
                    saveSearchResult = body.data.rows; 
                    $searchResult.html(liveHtmlChange(body.data.rows));
                    bindSearchItemTap();
                    body = null;
                } else {
                    $searchResult.html(liveHtmlChange());
                    saveSearchResult = [];
                }
            })
            $searchLabels.hide();
            $searchResult.show();
            
        }
    })

    $('#searchInput').on('keydown',function(e){
        if(e.keyCode === 13) {
            setTimeout(function(){
                if(saveSearchResult.length > 0) 
                    submitSearchResult(saveSearchResult[0])         
            }, 200);
        }
    })

    $('#searchCancel,#searchClear').on('tap', function () {
        $searchResult.hide().html();
        $searchLabels.show();
    })

    function bindSearchItemTap() {
        $searchResult.find('.search-result-item').on('tap',function(){
            var $this = $(this);
            var index = $this.data('index');
            submitSearchResult(saveSearchResult[index]);
        })
    }

    function getSearchPoints(searchName, cb) {
        $.get('/publicMap/getServicePoint?serviceName=' + searchName)
            .success(function (body) {
                console.log(body)
                if (body.status === 200) {
                    // $searchResult.html(liveHtmlChange(body.data.rows))
                    cb && cb(body)
                    body = null;
                }
            })
            .fail(function (err) {
                console.log(err)
            })
    }

    function submitSearchResult(data){
        $.post('/search/saveSearchRes', data)
            .success(function (body) {

                if (body.status === 200) {
                    var acc = getCookie('acc');
                    window.location.href = '/publicMap/index?acc=' + acc;
                }
            })
            .fail()
    }
})

function liveHtmlChange(data) {
    var liveDom = '';
    if (data && data instanceof Array && data.length > 0) {
        for (var i = 0; i < data.length; i++) {
            liveDom += '<div data-index='+i+' class="weui-cell weui-cell_access search-result-item"><div class="weui-cell__bd weui-cell_primary"><p>' + data[i].serviceName + '</p></div></div>'
        }
    } else {
        liveDom = '<div data-index=' + i + ' class="weui-cell weui-cell_access search-result-item"><div class="weui-cell__bd weui-cell_primary"><p>未搜索到相关内容</p></div></div>'
    }
    return liveDom;
}
